package com.asisto.controller

import com.asisto.model.*
import java.text.SimpleDateFormat
import grails.converters.*


class JornadaController {
    
    def index = { redirect(action:list,params:params) }

    // the delete, save and update actions only accept POST requests
    static allowedMethods = [delete:'POST', save:'POST', update:'POST']

    def calendarioSemanal = {}

    def list = {
        params.max = Math.min( params.max ? params.max.toInteger() : 10,  100)
        [ jornadaInstanceList: Jornada.list( params ), jornadaInstanceTotal: Jornada.count() ]
    }

    def show = {
        def jornadaInstance = Jornada.get( params.id )

        if(!jornadaInstance) {
            flash.message = "Jornada not found with id ${params.id}"
            redirect(action:list)
        }
        else { return [ jornadaInstance : jornadaInstance ] }
    }

    def delete = {
        def jornadaInstance = Jornada.get( params.id )
        if(jornadaInstance) {
            try {
                jornadaInstance.delete(flush:true)
                flash.message = "Jornada ${params.id} deleted"
                redirect(action:list)
            }
            catch(org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "Jornada ${params.id} could not be deleted"
                redirect(action:show,id:params.id)
            }
        }
        else {
            flash.message = "Jornada not found with id ${params.id}"
            redirect(action:list)
        }
    }

    def edit = {
        def jornadaInstance = Jornada.get( params.id )

        if(!jornadaInstance) {
            flash.message = "Jornada not found with id ${params.id}"
            redirect(action:list)
        }
        else {
            return [ jornadaInstance : jornadaInstance ]
        }
    }

    def update = {
        def jornadaInstance = Jornada.get( params.id )
        if(jornadaInstance) {
            if(params.version) {
                def version = params.version.toLong()
                if(jornadaInstance.version > version) {
                    
                    jornadaInstance.errors.rejectValue("version", "jornada.optimistic.locking.failure", "Another user has updated this Jornada while you were editing.")
                    render(view:'edit',model:[jornadaInstance:jornadaInstance])
                    return
                }
            }
            jornadaInstance.properties = params
            if(!jornadaInstance.hasErrors() && jornadaInstance.save()) {
                flash.message = "Jornada ${params.id} updated"
                redirect(action:show,id:jornadaInstance.id)
            }
            else {
                render(view:'edit',model:[jornadaInstance:jornadaInstance])
            }
        }
        else {
            flash.message = "Jornada not found with id ${params.id}"
            redirect(action:list)
        }
    }

    def create = {
        def jornadaInstance = new Jornada()
        jornadaInstance.properties = params
        return ['jornadaInstance':jornadaInstance]
    }

    def save = {
        def jornadaInstance = new Jornada(params)
        if(!jornadaInstance.hasErrors() && jornadaInstance.save()) {
            flash.message = "Jornada ${jornadaInstance.id} created"
            redirect(action:show,id:jornadaInstance.id)
        }
        else {
            render(view:'create',model:[jornadaInstance:jornadaInstance])
        }
    }


    def semanajson = {

        Date fechaStart = null
        Date fechaEnd = null

        println "params = $params"

        def start = params?.start
        def end = params?.end

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd")

        if (start) {
            fechaStart = sdf.parse(start)
        }

        if (end) {
            fechaEnd = sdf.parse(end)
        }

        println "busca eventos entre ${fechaStart} y ${fechaEnd}"

        def eventos = []

        params.max = 20
        eventos = Jornada.list(params)

        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'+10:00'")

        def listaJson = []
        listaJson = eventos.collect {
            println "evento: ${it.id} - fecha: ${it.fecha} - start: ${it.horaInicio} - end: ${it.horaFin}"


            SimpleDateFormat sFechaEstandar = new SimpleDateFormat("dd/MM/yyyy HH:mm")
            Date fechaJornada1 = sFechaEstandar.parse(it.fecha + " " + it.horaInicio + ":00")
            Date fechaJornada2 = sFechaEstandar.parse(it.fecha + " " + it.horaFin + ":00")
            
            def inicio = sdf2.format(fechaJornada1)
            def fin = sdf2.format(fechaJornada2)

            new EventoJson(id: it.id, start:inicio, end:fin, title:it.descripcion, clase:'tarea')
        }

        println "lista objetos json"
        listaJson.each {
            println "evento: ${it.id} - start: ${it.start} - end: ${it.end}"
        }

        render listaJson as JSON
    }

}
